class Solution {
public:
    vector<int> grayCode(int n) {
        if (n==0) return vector<int>(1, 0);
        vector<int> ans;
        ans.push_back(0);
        ans.push_back(1);
        for (int i=1;i<n;i++)
            for (int j=(1<<i)-1;j>=0;j--)
                ans.push_back(ans[j]+(1<<i));
        return ans;
    }
};
